/****************************************************************************
 * 2025.6 GRAVEROBBER-PC
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.Rendering.Universal;
using UnityEngine.U2D;

namespace ProjectSurvival
{
    public partial class TreasurePanel : UIElement, IController
    {
        ResLoader _resLoader = ResLoader.Allocate();
        private void Start()
        {
            BtnConfirm.onClick.AddListener(() =>
            {
                Time.timeScale = 1f;
                this.Hide();
            });
        }

        private void OnEnable()
        {
            LevelUpSystem levelUpSystem = this.GetSystem<LevelUpSystem>();

            var matchedPairedItem = levelUpSystem.LevelUpItems.Where(item =>
            {
                if (item.currentLevel.Value >= 7)
                {
                    var containsInPair = levelUpSystem.Pairs.ContainsKey(item.Key);
                    var pairedItemKey = levelUpSystem.Pairs[item.Key];
                    var pairedItemStartUpgrade = levelUpSystem.Dictionary[pairedItemKey].currentLevel.Value > 0;
                    var pairedUnlocked = levelUpSystem.PairedProperties[item.Key].Value;
                    return containsInPair && pairedItemStartUpgrade && !pairedUnlocked;
                }

                return false;
            });
            //如果条件满足解锁超级武器则执行超级武器解锁逻辑，否则执行之前的宝箱逻辑；
            Debug.Log("可解锁超级武器数量："+matchedPairedItem.Count()+"any:"+matchedPairedItem.Any());
            if (matchedPairedItem.Any())
            {
                var item = matchedPairedItem.ToList().GetRandomItem();
                Content.text = $"<b>合成后的{item.Name}</b>解锁！\n";
                while (!item.UpgradeFinish)
                {
                    item.Upgrade();
                }
                
                levelUpSystem.PairedProperties[item.Key].Value = true;
                _resLoader.Add2Load<SpriteAtlas>("Icon");
                _resLoader.LoadAsync(() =>
                {
                    Icon.sprite = _resLoader.LoadSync<SpriteAtlas>("Icon").GetSprite(item.PairedIconName);
                });
                //Icon.sprite = _resLoader.LoadSync<SpriteAtlas>("Icon").GetSprite(item.PairedIconName);
                Icon.Show();
            }
            else
            {
                var levelUpItems = levelUpSystem.LevelUpItems
                    .Where(item => item.currentLevel.Value > 0 && !item.UpgradeFinish).ToList();
                if (levelUpItems.Count > 0)
                {
                    var item = levelUpItems.GetRandomItem();
                    Content.text = item.Description;
                    
                    Icon.sprite = _resLoader.LoadSync<SpriteAtlas>("Icon").GetSprite(item.IconName);
                    Icon.Show();
                    
                    item.Upgrade();
                }
                else
                {
                    if (Global.Hp.Value < Global.MaxHp.Value)
                    {
                        Content.text = "恢复1血量";
                        AudioKit.PlaySound(QAssetBundle.Hp_wav.HP);
                        Global.Hp.Value++;
                        Icon.Hide();
                        return;
                    }

                    Content.text = "增加50金币";
                    Global.Coin.Value += 50;
                    Icon.Hide();
                }
            }
            Debug.Log(
                $"SuperBombState:{Global.SuperBomb.Value}\r\n" +
                $"SuperBasketBallState:{Global.SuperBasketBall.Value}\r\n" +
                $"SuperKnifeState:{Global.SuperKnife.Value}\r\n" +
                $"SuperSwordState:{Global.SuperSword.Value}\r\n" +
                $"SuperRotateSwordState:{Global.SuperRotateSword.Value}\r\n" 
            );
        }

        protected override void OnBeforeDestroy()
        {
            base.OnBeforeDestroy();
            _resLoader.Recycle2Cache();
            _resLoader = null;
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}